<?php

namespace App\Model\FBA;

//use App\Model\WMS\OrderItem\Tpl;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
//use App\Model\WMS\OrderItem as SdWms;
//use \App\Contracts\MWS\Model as WMSModel;

class InboundShipmentItem extends Model /*implements SdWms\Unit*/
{

	use \App\Common\Eloquent\Query;
	use \App\Common\Eloquent\Model\CompositePrimaryKey;
    use \jdavidbakr\ReplaceableModel\ReplaceableModel;


    protected $table = 'fba_inbound_shipment_item';
    public $timestamps = false;
    public $incrementing = false;

    public $primaryKey = [
    	'ShipmentId', 'product_id'
    ];



    protected $fillable = [
        'ShipmentId',
        'SellerSKU',
	    'FulfillmentNetworkSKU',
        'QuantityShipped',
        'QuantityInCase',
        'product_id',
    ];


	use \Peak\Plugin\Debuger\Base;


//    protected static function boot()
//    {
//        parent::boot();
//
//		static::addGlobalScope('QuantityShipped', function (Builder $builder){
//			$builder->where('QuantityShipped','>', 0);
//		});
//    }


    const MAX_OF_QUANTITY_IN_CASE = 150;


    /**
     * 设置装箱率 并判断装箱率是否合法
     * @param $val
     */
    public function setQuantityInCaseAttribute ($val)
    {

        if ($val>self::MAX_OF_QUANTITY_IN_CASE && is_prime_int($val)) {
            $val = null;
            return self::debug('装箱率为超过'.self::MAX_OF_QUANTITY_IN_CASE.'的质数，需要调整发货数量。');
        } else {
            $val = (int)$val;
            while($val>self::MAX_OF_QUANTITY_IN_CASE||!is_int($val)) {
                isset($ctn) || $ctn=2;
                $val = $this->QuantityShipped/$ctn;
                $ctn++;
            }
        }

        $this->attributes['QuantityInCase'] = $val;
    }





    /**
     * 将Model转成亚马逊接口格式
     * @param $isPlan bool true(默认)对应CreateInboundShipment接口格式，false对应CreateInboundShipmentPlan接口格式。
     * @param $toArray boolean true输出数组，false，输出对象
     * @return array|object
     * */
	public function toMwsTpl ($isPlan=true, $toArray=true)
	{
		if ($isPlan) {
			$dat = [
				'SellerSKU' => $this->SellerSKU,
				'Quantity' => $this->QuantityShipped,
				'QuantityInCase' =>$this->QuantityInCase,
			];
		} else {
			$dat = [
				'ShipmentId' => $this->ShipmentId,
				'SellerSKU' => $this->SellerSKU,
				'FulfillmentNetworkSKU' => $this->FulfillmentNetworkSKU,
				'QuantityShipped' => $this->QuantityShipped,
				'QuantityInCase' => $this->QuantityInCase,
			];
		}
		return $toArray ? $dat : (object)$dat;
	}



	//	use WMSModel;

	####### relationship


    /**
     * 关联商品
     *
     * */
	public function product()
	{
		return $this->belongsTo(\App\Model\Amz\Product::class, 'SellerSKU', 'sku')->whereEcId(InboundShipment::ecId());
	}


	/**
	 * 关联·FBA入库单Shipment
	 * */
	public function shipment()
	{
		return $this->belongsTo(InboundShipment::class, 'ShipmentId', 'ShipmentId');
	}



    public function shipmentPlan()
    {
        return $this->hasOne('\App\Model\Shipment\Plan', 'id', 'plan_id');
    }

    public function twusaInventory()
    {
        return $this->hasOne('\App\Model\TWUsa\TwusaInventory', 'product_sn', 'FulfillmentNetworkSKU');
    }


    //关联采购 装箱率
//    public function ctnQty()
//    {
//        return $this->hasOne('\App\Model\Shipment\Plan', 'sku', 'SellerSKU')->orderByDesc('ship_date');
//
//    }


	####### extension



	public function toWmsOrderItem(): SdWms\Tpl
	{
		return new SdWms\Tpl([

		]);
	}


}
